//
//  WheelView.m
//  赞着买
//
//  Created by dllo on 15/10/7.
//  Copyright (c) 2015年 MyNews. All rights reserved.
//

#import "WheelView.h"
#import <SDWebImageManager.h>
#import <UIImageView+WebCache.h>

#pragma 创建需要的控件,延展封装部分控件
@interface WheelView ()<UIScrollViewDelegate>

@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIPageControl *pageControll;
@property (nonatomic, retain) NSTimer *timer;

@end

@implementation WheelView

- (void)dealloc
{
    [_scrollView release];
    [_pageControll release];
    [_timer release];
    [super dealloc];
}

#pragma 使用初始化方法,创建界面
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.wheelArr = [NSMutableArray array];
        [self creat];
    }
    return self;
}
- (void)creat
{
    //使用get方法
    [self addSubview:self.scrollView];
    [self addSubview:self.pageControll];
    
    
    //定时器,只创建一次,,在layoutSubviews会出现Bug
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(changeScroller) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
}

#pragma 懒加载pageControll(Get方法)
- (UIPageControl *)pageControll
{
    if (nil == _pageControll)
    {
        self.pageControll = [[UIPageControl alloc] initWithFrame:CGRectMake((self.frame.size.width - 120) / 2, self.frame.size.height - 30, 120, 30)];
        self.pageControll.currentPage = 0;
        [self.pageControll addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
        self.pageControll.defersCurrentPageDisplay = YES;
        [_pageControll release];
    }
    return _pageControll;
}
#pragma 懒加载scrollView(Get方法)
- (UIScrollView *)scrollView
{
    if (nil == _scrollView)
    {
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        self.scrollView.pagingEnabled = YES;
        self.scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
        self.scrollView.delegate = self;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        [_scrollView release];
    }
    return _scrollView;
}

#pragma 父类方法,,最后执行,给控件传值
- (void)layoutSubviews
{
    [super layoutSubviews];

    //给scrollView设置图片
    if (self.wheelArr.count != 0) {
        for (NSInteger i = 0; i < self.wheelArr.count + 2; i++)
        {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake([[UIScreen mainScreen] bounds].size.width * i, 0, [[UIScreen mainScreen] bounds].size.width, (667 - 60) / 3 * KPropotionHeight)];
            imageView.userInteractionEnabled = YES;
            
            if (0 == i)
            {
                NSLog(@"🍎%ld", self.wheelArr.count);
                NSLog(@"=====%@", [[self.wheelArr objectAtIndex:self.wheelArr.count - 1 ]picUrl]);
                [imageView sd_setImageWithURL:[NSURL URLWithString:[[self.wheelArr objectAtIndex:self.wheelArr.count - 1 ]picUrl]]];
            } else if (self.wheelArr.count + 1 == i) {
                [imageView sd_setImageWithURL:[NSURL URLWithString:[[self.wheelArr objectAtIndex:0] picUrl]]];
            } else {
                [imageView sd_setImageWithURL:[NSURL URLWithString:[[self.wheelArr objectAtIndex:i - 1]  picUrl]]];
            }
            [self.scrollView addSubview:imageView];
            [imageView release];
            
            UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
            [imageView addGestureRecognizer:tapGestureRecognizer];
            [tapGestureRecognizer release];        
        }
        //给控件赋值
        self.pageControll.numberOfPages = self.wheelArr.count;
        self.scrollView.contentSize = CGSizeMake(self.frame.size.width * (self.wheelArr.count + 2), 0);
    }
   
}

- (void)tapAction:(UITapGestureRecognizer *)sender
{
   NSInteger num = self.scrollView.contentOffset.x / self.frame.size.width;
    if (0 == num)
    {
        num = self.wheelArr.count;
    }
    if (self.wheelArr.count + 1 == num)
    {
        num = 1;
    }
    [self.delegatePush pushToWheelDetailView:[self.wheelArr objectAtIndex:num - 1]];
}


#pragma 定时器调用方法,当偏移量为边界时使用动画效果将scrollView移至尾页后不使用动画将界面移至最前
- (void)changeScroller
{
    CGPoint offset = self.scrollView.contentOffset;
    NSInteger current = self.scrollView.contentOffset.x / self.frame.size.width;
    if (self.wheelArr.count == current)
    {
        self.pageControll.currentPage = 0;
        offset.x += self.frame.size.width;
        [self.scrollView setContentOffset:offset animated:YES];
        self.scrollView.contentOffset = CGPointMake(0, 0);
    } else {
        self.pageControll.currentPage += 1;
        offset.x += self.frame.size.width;
        [self.scrollView setContentOffset:offset animated:YES];
        self.scrollView.contentOffset = CGPointMake(self.pageControll.currentPage * self.frame.size.width, 0);
    }
}

#pragma pageControl执行方法
- (void)pageAction:(UIPageControl *)sender
{
    CGPoint offset = CGPointMake(self.frame.size.width * (self.pageControll.currentPage + 1), 0);
    [self.scrollView setContentOffset:offset animated:YES];
}

#pragma 在减速结束时调用该方法,设置边界值跳转图片,重新设定定时器
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.pageControll.currentPage = self.scrollView.contentOffset.x / self.frame.size.width - 1;
    NSInteger current = self.scrollView.contentOffset.x / self.frame.size.width;
    if (0 == current)
    {
        self.pageControll.currentPage = 5;
        self.scrollView.contentOffset = CGPointMake(self.frame.size.width * self.wheelArr.count, 0);
    } else if (self.wheelArr.count + 1 == current) {
        self.pageControll.currentPage = 0;
        self.scrollView.contentOffset = CGPointMake(self.frame.size.width , 0);
    }
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(changeScroller) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
}

#pragma 在拖拽开始时,关闭定时器
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.timer invalidate];
    self.timer = nil;
}
@end

